<?php

namespace app\admin\controller;

use leruge\auth\AuthFacade;
use think\Controller;

class Home extends Controller
{
    // 无权限框架
    public function wdlHome()
    {
        return view('home');
    }

    // 无权限首页
    public function wdlIndex()
    {
        return view('index');
    }

    // 左侧菜单列表
    public function wdlLeftMenuList()
    {
        // 首页信息
        $homeInfo = [
            'title' => '首页',
            'href' => url('admin/home/wdlIndex')
        ];

        // logo信息
        $systemInfo = model('System')->find();
        $logoInfo = [
            'title' => $systemInfo['web_name'],
            'image' => $systemInfo['logo'],
            'href' => ''
        ];
        $userRuleIdArray = AuthFacade::userRuleIdList(session('adminId'));
        $menuList = model('AuthRule')::field(['id', 'pid', 'title', 'icon', 'name' => 'href'])->where('id', 'in', $userRuleIdArray)->where('is_show', 1)->order('sort', 'asc')->withAttr([
            'target' => function () {
                return '_self';
            },
            'icon' => function ($value) {
                return 'fa ' . $value;
            }
        ])->select()->append(['target'])->toArray();
        $menuList = $this->buildMenuList(0, $menuList);
        // 菜单信息
        $menuInfo = [[
            'title' => '常规管理',
            'icon' => 'fa fa-address-book',
            'href' => '',
            'target' => '_self',
            'child' => $menuList
        ]];
        $info = [
            'homeInfo' => $homeInfo,
            'logoInfo' => $logoInfo,
            'menuInfo' => $menuInfo
        ];
        return json($info);
    }

    // 清除缓存
    public function wdlClearCache()
    {
        $path = env('runtime_path');
        if (is_dir($path)) {
            $p = scandir($path);
            foreach($p as $val){
                if($val !="." && $val !=".."){
                    if(is_dir($path.$val)){
                        $this->delDir($path.$val.'/');
                        @rmdir($path.$val.'/');
                    }else{
                        unlink($path.$val);
                    }
                }
            }
        }
        $this->success('清除成功');
    }

    // 无权限页面
    public function wdlNoAuth()
    {
        return view('no_auth');
    }

    // 删除文件夹
    private function delDir($path){
        if(is_dir($path)){
            $p = scandir($path);
            foreach($p as $val){
                if($val !="." && $val !=".."){
                    if(is_dir($path.$val)){
                        $this->delDir($path.$val.'/');
                        @rmdir($path.$val.'/');
                    }else{
                        unlink($path.$val);
                    }
                }
            }

        }
    }

    // 组装菜单
    private function buildMenuList($pid, $menuList)
    {
        $treeList = [];
        foreach ($menuList as $v) {
            if ($pid == $v['pid']) {
                $v['href'] = $v['href'] ? url($v['href']) : null;
                $node = $v;
                $child = $this->buildMenuList($v['id'], $menuList);
                if (!empty($child)) {
                    $node['child'] = $child;
                }
                $treeList[] = $node;
            }
        }
        return $treeList;
    }
}
